package com.neusoft.neps.method;

import com.neusoft.grid.entity.Mission;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class MissionIO {
    private static final String path = "src/com/neusoft/neps/data/";
    static public List<Mission> readMissions(String filename) throws IOException {
        File file = new File(path+filename);
        if (!file.exists() || file.length() == 0) {
            System.out.println("Data file missing or empty");
            file.createNewFile();
            return new ArrayList<>();
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (List<Mission>) ois.readObject();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }
    static public void writeMissions(List<Mission> missions,String filename) throws IOException {
        File file = new File(path+filename);
        if (!file.exists()) {
            file.createNewFile();
        }
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(missions);
        }
    }
}
